#include <stdio.h>
#include <stdlib.h>

int dir[4][2] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};

void dfs(int **grid, int **visited, int x, int y, int row, int col) {
    // 深度优先算法搜索策略是：搜索一个方向，直到该方向搜索完毕，再搜索另一个方向。
    for(int i = 0; i < 4; i++) {
        int nextX = x + dir[i][0];
        int nextY = y + dir[i][1];
        if (nextX < 0 || nextX >= row || nextY < 0 || nextY >= col) continue;
        if (visited[nextX][nextY] == 0 && grid[nextX][nextY] == 1) {
            visited[nextX][nextY] = 1;
            dfs(grid, visited, nextX, nextY, row, col);
        }
    }
};

int main(){
    int row, col;
    scanf("%d %d", &row, &col);

    int **grid = (int **)malloc(sizeof(int *) * row);
    for(int i = 0; i < row; i++) {
        grid[i] = (int *)malloc(sizeof(int) * col);
    }

    int **visited = (int **)malloc(sizeof(int *) * row);
    for(int i = 0; i < row; i++) {
        visited[i] = (int *)malloc(sizeof(int) * col);
    }

    for(int i = 0; i < row; i++) {
        for(int j = 0; j < col; j++) {
            visited[i][j] = 0;
            scanf("%d", &grid[i][j]);
        }
    }

    int count = 0;
    for(int i = 0; i < row; i++) {
        for(int j = 0; j < col; j++) {
            if (grid[i][j] == 1 && visited[i][j] == 0) {
                count++;
                visited[i][j] = 1;
                dfs(grid, visited, i, j, row, col);
            }
        }
    }

    printf("%d", count);

    for(int i = 0; i < row; i++) {
        free(grid[i]);
        free(visited[i]);
    }    
    free(grid);
    free(visited);

    return 0;
}